﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Events per Log";
    ViewBag.Breadcrumb = "Statistic > Events per Log";
}

<h2>Events per Log</h2>


<fieldset>
    <legend>Errors Chart</legend>
    <div id="chart" style="min-width: 800px; min-height: 600px;">
    </div>
</fieldset>

@section AsideLeft{
    @Html.Partial("StatisticNavigation")
}

@section Foot{
    <script src="@Url.Content("~/Scripts/highcharts.js")" type="text/javascript" ></script>
<script type="text/javascript">
var options, chart;
$(document).ready(function () {
    options = {
        chart: {
            renderTo: 'chart',
            defaultSeriesType: 'column'
        },
        title: {
            text: 'Events'
        },
        subtitle: {
            text: 'Render Date: @(DateTime.Now.ToShortDateString())'
        },
        xAxis: {
            categories: []
        },
        yAxis: {
            title: {
                text: 'Amount of Events'
            },
            min: 0
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b>' + this.y;
            }
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: []
    };

    function findMonth(arr, month) {
        var i = 0;
        var l = arr.length;
        for (i; i < l; i++) {
            if (arr[i].month === month) {
                break;
            }
        }
        if (i < l) {
            return arr[i];
        }
        return null;
    }

    //Get data with json
    $.getJSON('@(Url.Action("EventsPerLogColumnData", "Statistic"))', function (data) {
        $.each(data.categories, function (catkey, cat) {
            options.xAxis.categories.push(cat.text);
        });

        $.each(data.series, function (logkey, log) {
            var serie = {
                name: log.name,
                data: []
            };

            var i = 0;
            $.each(data.categories, function(catkey, cat) {
                var month = findMonth(log.data, cat.val);
                var count = month === null ? 0 : parseInt(month.count);
                serie.data.push(isNaN(count) ? 0 : count);
            });
            options.series.push(serie);
        });
        chart = new Highcharts.Chart(options);
    });
});   
</script>
}
